/**
 * @TlvDemo3.java  COPYRIGHT xiaojian
 * 
 * Created on 2018年7月24日 下午12:07:51
 */
package xj.toolkit.bean.tlv.test;

import java.util.ArrayList;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.common.collect.Lists;

import xj.toolkit.bean.tlv.decode.decoders.BeanTLVDecoder;
import xj.toolkit.bean.tlv.encode.encoders.BeanTLVEncoder;
import xj.toolkit.util.ByteUtils;

/**
 * @author <a href="mailto:cxj2000@gmail.com">xiaojian.cao</a>
 *
 */
public class TlvDemo3 {

	public static void main(String[] args) {
		AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
				"xj/toolkit/bean/tlv/test/tlvDemo2.xml");

		BeanTLVEncoder beanEncoder = (BeanTLVEncoder) ctx.getBean("beanEncoder");

		StatusTotal statusTotal = new StatusTotal();
		Status status1 = new Status();
		status1.s = 1;
		status1.desc = "1";

		Status status2 = new Status();
		status2.s = 2;
		status2.desc = "2";

		statusTotal.total.put("status1", new ArrayList(Lists.newArrayList(status1, status2)));
		statusTotal.total.put("status2", new ArrayList(Lists.newArrayList(status1, status2)));

		byte[] bytes = ByteUtils.union(beanEncoder.encode(statusTotal,
				beanEncoder.getEncodeContextFactory().createEncodeContext(StatusTotal.class, null)));

		System.out.println("TLV bytes is :\n" + ByteUtils.bytesAsHexString(bytes, 2048));

		BeanTLVDecoder beanDecoder = (BeanTLVDecoder) ctx.getBean("beanDecoder");
		Object bean = beanDecoder.decode(bytes.length, bytes,
				beanDecoder.getDecodeContextFactory().createDecodeContext(StatusTotal.class, null));
		System.out.println(bean);

	}
}
